package com.weipai.springboot.module.service.impl;

import com.github.pagehelper.PageInfo;
import com.weipai.springboot.enums.DemandStatus;
import com.weipai.springboot.enums.RetCode;
import com.weipai.springboot.enums.UserType;
import com.weipai.springboot.model.Artisan;
import com.weipai.springboot.model.Customer;
import com.weipai.springboot.model.SaleReturn;
import com.weipai.springboot.model.api.PageResult;
import com.weipai.springboot.model.api.Response;
import com.weipai.springboot.module.dao.*;
import com.weipai.springboot.module.mapper.SaleReturnMapper;
import com.weipai.springboot.module.service.DataStatisticsService;
import com.weipai.springboot.module.util.SessionUserUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @Author: 曹朋飞
 * @Date: 2018/09/17 14:24
 */
@Service
public class DataStatisticsServiceImpl implements DataStatisticsService{

    private static final Logger log = LoggerFactory.getLogger(DataStatisticsServiceImpl.class);

    @Autowired
    private OrderDao orderDao;
    @Autowired
    private ProductDao productDao;
    @Autowired
    private CustomerDao customerDao;
    @Autowired
    private ShopDao shopDao;
    @Autowired
    private GoodsDao goodsDao;
    @Autowired
    private ArtisanDao artisanDao;
    @Autowired
    private DemandDao demandDao;
    @Autowired
    private SessionUserUtil sessionUserUtil;



    @Override
    public Response getHomepageData() {
        Response response = new Response();
        //各种状态的订单
        List<Map<String, Object>> list = orderDao.getOrderCountList(null);
        response.put("orderNumber", list);
        //查询商品的数量
        response.put("goods1",productDao.getProductAmount(1));//上架
        response.put("goods2",productDao.getProductAmount(-1));//下架
        response.put("goods3",productDao.getProductAmount(-2));//回收站
        response.put("goods4",productDao.getProductAmount(2));//总数量
        //商品的总评价数
        if(null!=productDao.getEvaluateAmount()){
            response.put("evaluateAmount",productDao.getEvaluateAmount());
        }else{
            response.put("evaluateAmount",0);
        }
        //商户用户总数
        response.put("customerAmount",customerDao.getCustomerAmount());
        //查询总共的交易金额
        response.put("realPay",orderDao.getAllRealPay(null, null));
        response.setCode(RetCode.SUCCESS);
        return response;
    }



    //订单报表
    @Override
    public Response orderStatement(String time) {
        Response response = new Response();
        //按月份和订单状态分组查出交易金额
        List<Map<String, Object>> map = orderDao.orderStatement(time);
        response.put("statement", map);
        //各种状态的订单
        List<Map<String, Object>> list = orderDao.getOrderCountList(time);
        response.put("source", list);
        //退款金额
        Integer saleReturnRealPay = orderDao.getSaleReturnRealPay(time);
        if(null!=saleReturnRealPay){
            response.put("saleReturnRealPay", saleReturnRealPay);
        }else{
            response.put("saleReturnRealPay", 0);
        }
        response.setCode(RetCode.SUCCESS);
        return response;
    }

    //商家结算报表
    @Override
    public Response shopStatement(Integer page, Integer size, String draw) {
        Response response = new Response();
        List<Map<String, Object>> shopMap = shopDao.getShopStatement((page-1)*size,size);
        if(null!=shopMap){
            int amount = shopDao.getShopStatementAmount();
            int size01 = shopMap.size();
            for(int i=0;i<size01;i++){
                shopMap.get(i).put("orderAmount", orderDao.getOrderByShopId(shopMap.get(i).get("shopId").toString()));
                Integer saleReturnRealPay = orderDao.getSaleReturnRealPayByShopId(shopMap.get(i).get("shopId").toString());
                if(null!=saleReturnRealPay){
                    shopMap.get(i).put("saleReturnRealPay", saleReturnRealPay);
                }else{
                    shopMap.get(i).put("saleReturnRealPay", 0);
                }
            }
            PageResult<Map<String, Object>> pageList = new PageResult<>(shopMap,page,size,amount, draw);
            
            response.setCode(RetCode.SUCCESS);
            response.put("pageList", pageList);
        }else{
            response.setCode(RetCode.NO_DATA);
        }
        return response;
    }

    //需求报表
    @Override
    public Response demandStatement(String time, String demandCategoryId) {
        Response response = new Response();

        //按月份和需求订单状态分组查出每个月各个订单的数量
        List<Map<String, Object>> map = demandDao.demandStatement(time, demandCategoryId);
        response.put("statement", map);
        //每个分类下的订单数量
        List<Map<String, Object>> list = demandDao.getDemandCountList(time, DemandStatus.COMPLETED.getStatus());
        response.put("demandCount", list);
        response.setCode(RetCode.SUCCESS);
        return response;
    }

    //商品报表
    @Override
    public Response goodsStatement(Integer page, Integer size, String draw) {
        Response response = new Response();
        List<Map<String, Object>> goodsMap = goodsDao.goodsStatement((page-1)*size,size);
        if(null!=goodsMap){
            int amount = goodsDao.getGoodsAmount();
            PageResult<Map<String, Object>> pageList = new PageResult<>(goodsMap,page,size,amount, draw);
            response.setCode(RetCode.SUCCESS);
            response.put("pageList", pageList);
        }else{
            response.setCode(RetCode.NO_DATA);
        }
        return response;
    }

    //员工的收益报表
    @Override
    public Response employeeStatement(Integer page, Integer size, String startTime, String endTime, String draw, String customerId) {
        Response response = new Response();
        Integer start = (page-1) * size;
//        if(null !=customerId){
//            customerId = sessionUserUtil.getAdminAttr().getAdminId();
//        }
        List<Map<String, Object>> employeeComeIn = customerDao.getMyIncomesList(customerId, start, size, startTime, endTime);
        Integer amount = customerDao.getMyIncomeAmount(customerId, startTime, endTime);
        PageResult<Map<String, Object>> pageList = new PageResult<>(employeeComeIn,page,size,amount, draw);
        Integer allMoney = customerDao.getEmployeeInComeMoney(customerId, startTime, endTime);
        Integer dataMoney = customerDao.getEmployeeInComeMoney(customerId, "today", "today");
        
        response.setCode(RetCode.SUCCESS);
        response.put("pageList", pageList);
        response.put("allMoney", allMoney);
        response.put("dataMoney", dataMoney);
        return response;
    }

    //所有员工的收益
    @Override
    public Response allEmployeeStatement(Integer page, Integer size, String draw) {
        Response response = new Response();
        Integer start = (page-1) * size;
        List<Map<String, Object>> employeeMap = customerDao.list(start, size, String.valueOf(UserType.EMPLOYEE.getType()), null);
        Integer employeeAmount = customerDao.listByPageAmount(String.valueOf(UserType.EMPLOYEE.getType()), null);
        if(!employeeMap.isEmpty()){
            int size01 = employeeMap.size();
            for(int i=0;i<size01;i++){
                Integer allMoney = customerDao.getEmployeeInComeMoney(employeeMap.get(i).get("customerId").toString(), null, null);
                employeeMap.get(i).put("allMoney", allMoney);
            }
            PageResult<Map<String, Object>> pageList = new PageResult<>(employeeMap,page,size,employeeAmount, draw);
            response.put("pageList", pageList);
            response.setCode(RetCode.SUCCESS);
        }else{
            response.setCode(RetCode.NO_DATA);
        }
        return response;
    }

    @Override
    public Response artisanStatement(Integer page, Integer size, String draw) {
        Response response = new Response();
        List<Map<String, Object>> list = artisanDao.selectAllByDel(0, "paging", "nosort", "noway", "1","-1");
        PageInfo<Map<String, Object>> pageInfo = new PageInfo<>(list);
        if (list.size() > 0) {
            int size01 = list.size();
            for(int i=0;i<size01;i++){
//                Integer allMoney = artisanDao.getArtisanEaringsByArtisanIdAmount(list.get(i).get("artisanId").toString(), null, null);
                Customer customerId = customerDao.getCustomerById(list.get(i).get("customerId").toString());
                list.get(i).put("allMoney", customerId.getCashMoney());
                Integer demandAmount = demandDao.getDemandAmountByArtisan(list.get(i).get("artisanId").toString(), DemandStatus.COMPLETED.getStatus());
                list.get(i).put("demandAmount", demandAmount);
            }
            PageResult<Map<String, Object>> pageList = new PageResult<>(list, page, size, (int) pageInfo.getTotal(), draw);
            response.setCode(RetCode.SUCCESS);
            response.put("pageList", pageList);
        } else {
            response.setCode(RetCode.NO_DATA);
        }
        return response;
    }

    
    @Override
    public Response artisanInfoStatement(Integer page, Integer size, String startTime, String endTime, String draw, String artisanId) {
        Response response = new Response();
        Integer start = (page-1) * size;
        List<Map<String, Object>> earingsMap = artisanDao.getArtisanEaringsByArtisanId(artisanId, start, size, startTime, endTime);
        Integer amount = artisanDao.getArtisanEaringsByArtisanIdAmount(artisanId, startTime, endTime);
        if(amount>=1){
            PageResult<Map<String, Object>> pageList = new PageResult<>(earingsMap,page,size,amount, draw);
            Integer allMoney = artisanDao.getArtisanMoney(artisanId, startTime, endTime);
            Integer dataMoney = artisanDao.getArtisanMoney(artisanId, "today", "today");

            response.setCode(RetCode.SUCCESS);
            response.put("pageList", pageList);
            response.put("allMoney", allMoney);
            response.put("dataMoney", dataMoney);
        }else{
            response.setCode(RetCode.NO_DATA);
        }
        return response;
    }
}
